﻿#include "map_osg_osgtool.h"
#include "../map_globaldata/map_globaldata_appdata.h"
#include <osgEarth/GeoMath>

double MAP_OSG_OSGTool::getHeight(double longitude, double latitude)
{
    double height = 0.0;
    auto l_mapData = MAP_GLOBALDATA_APPData::getInstance()->g_mapData;
    if(!l_mapData->valid)
        return -1;
    auto node = l_mapData->pMapNode;
    node->getTerrain()->getHeight(node->getMapSRS(), longitude, latitude, &height);
    return height;
}

void MAP_OSG_OSGTool::setViewPointToRegion(double xMin, double yMin, double xMax, double yMax)
{
    auto l_mapData = MAP_GLOBALDATA_APPData::getInstance()->g_mapData;
    if(!l_mapData->valid)
        return;

    auto e = l_mapData->pEarthManipulator;
    //中心经度
    double xMid = (xMin + xMax) / 2;
    //中心纬度
    double yMid = (yMin + yMax) / 2;

    osg::Vec3d p1(xMid, yMid, 0);
    osg::Vec3d p2(xMid, yMin, 0);

    double dyWorld = osgEarth::GeoMath::distance(p1, p2, l_mapData->pMapSRS);
    double fov = e->getLastKnownVFOV();
    double tanFov  = tan(osg::DegreesToRadians (fov / 2) );
    double height = dyWorld / tanFov;

    osgEarth::Viewpoint v( "Init Region",  xMid, yMid, 0.0, 0.0, -90.0, height );
    e->setViewpoint(v, 1.0);
}

MAP_OSG_OSGTool::MAP_OSG_OSGTool()
{

}
